using Admin2025.Domain.Repositories;
using Admin2025.Infrastructure.Data;
using Admin2025.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Admin2025.Infrastructure;

// 服务容器扩展，用于注册Admin2025DbContext。然后再api中调用
public static class ServiceCollectionExtenstion
{
  // 想让他变得可以链式调用，所以返回。但是不反悔也可以
  // this... ：指明扩展方法是为谁写的
  public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
  {
    // 注册DbContext，使用postgreSQL
    var connectionString = configuration.GetConnectionString("pgsql");
    services.AddDbContext<Admin2025DbContext>(x =>
    {
      x.UseNpgsql(connectionString);
    });

    // 注册仓储实现
    services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));

    return services;
  }
}